package cn.com.jslh.boarpro.main.view.fragment;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.dlazaro66.qrcodereaderview.QRCodeReaderView;
import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.base.BaseApplication;
import cn.com.jslh.boarpro.base.BaseFragment;
import cn.com.jslh.boarpro.base.mvp.BaseListView;
import cn.com.jslh.boarpro.main.presenter.AddBreedingRecordPresenterImpl;
import cn.com.jslh.boarpro.main.view.ChosePersonActivity;
import cn.com.jslh.boarpro.util.InputDialogUtil;
import cn.com.jslh.boarpro.util.ScanDialog;
import cn.com.jslh.boarpro.util.StartActivityUtil;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * 作者：周洲 on 2017/7/26 10:42
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class AddBreedingRecordFragment extends BaseFragment implements BaseListView {

    @BindView(R.id.muzhuTextView)
    TextView muzhuTextView;
    @BindView(R.id.gongzhuTextView1)
    TextView gongzhuTextView1;
    @BindView(R.id.gongzhuTextView2)
    TextView gongzhuTextView2;
    @BindView(R.id.gongzhuTextView3)
    TextView gongzhuTextView3;
    @BindView(R.id.peiZhongType)
    TextView peiZhongType;
    @BindView(R.id.pzy)
    TextView pzy;
    private AddBreedingRecordPresenterImpl addBreedingRecordPresenter;
    private Bundle savedInstanceState;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            this.savedInstanceState = savedInstanceState;
        }
    }

    public static String[] peizhongTypes = {"正常配种", "强配", "激素配种"};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        new RxPermissions(getActivity())
                .request(Manifest.permission.CAMERA)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(@NonNull Boolean aBoolean) throws Exception {
                        if (!aBoolean) {
                            Toast.makeText(getContext(), "未获取摄像头权限无法使用扫描二维码功能", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
        View view = inflater.inflate(R.layout.add_breeding_record_layout, container, false);
        unbinder = ButterKnife.bind(this, view);
        peiZhongType.setText(peizhongTypes[0]);
        peiZhongType.setTag("1");
        addBreedingRecordPresenter = new AddBreedingRecordPresenterImpl(getContext(), this);
        addBreedingRecordPresenter.upDatas();
        InfoShowFragment.initInfoShowFragment(getFragmentManager());//加载显示猪号信息
        return view;
    }

    @Override
    public void onItemClicked(String... params) { //数据保存成功
        muzhuTextView.setText("请点击扫描录入");
        muzhuTextView.setTag(null);
//        gongzhuTextView1.setText("请点击扫描录入");
//        gongzhuTextView1.setTag(null);
//        gongzhuTextView2.setText("请点击扫描录入");
//        gongzhuTextView2.setTag(null);
//        gongzhuTextView3.setText("请点击扫描录入");
//        gongzhuTextView3.setTag(null);
        peiZhongType.setText("正常配种");
        peiZhongType.setTag("1");
//        bakEdit.setText("");
        Toast.makeText(getContext(), params[0], Toast.LENGTH_SHORT).show();
        InfoShowFragment.getPigInfo(null, InfoShowFragment.Clear);
        BreedingRecordFragment.FlashUI();//更新记录页面数据
    }

    @OnClick({R.id.muzhuTextView, R.id.gongzhuTextView1,
            R.id.subbtn,
            R.id.gongzhuTextView2, R.id.gongzhuTextView3, R.id.peiZhongType,
            R.id.muzhuInput, R.id.gongzhuTextView1Input, R.id.gongzhuTextView2Input, R.id.gongzhuTextView3Input,
            R.id.pzy
    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.muzhuTextView:
                new ScanDialog.Builder(getContext())
                        .setQrCodeReadListener(new QRCodeReaderView.OnQRCodeReadListener() {
                            @Override
                            public void onQRCodeRead(String text, PointF[] points) {
                                Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
                                InfoShowFragment.getPigInfo(new InfoShowFragment.getInfoSuccessListener() {
                                    @Override
                                    public void success(String s, String id) {
                                        muzhuTextView.setText(s);
                                        muzhuTextView.setTag(id);
                                    }
                                }, InfoShowFragment.MuZhuType, text, SPUtils.getInstance().getString(BaseApplication.FLINEID));
                            }
                        }).create().show();
                break;
            case R.id.gongzhuTextView1:
                InputDialogUtil.showInputDialog(getContext(), "请输入耳号", gongzhuTextView1.getText().toString(), new InputDialogUtil.DialogListener() {
                    @Override
                    public void getText(String s) {
                        if (isHave(s)) {
                            Toast.makeText(getContext(), "已有此公猪", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (clearEditText(gongzhuTextView1, s)) {
                            InfoShowFragment.getPigInfo(null, InfoShowFragment.Clear);
                            return;
                        }
                        InfoShowFragment.getPigInfo(new InfoShowFragment.getInfoSuccessListener() {
                            @Override
                            public void success(String s, String id) {
                                gongzhuTextView1.setText(s);
                                gongzhuTextView1.setTag(id);
                            }
                        }, InfoShowFragment.GongZhuType, s, SPUtils.getInstance().getString(BaseApplication.FLINEID));
                    }
                });
                break;
            case R.id.gongzhuTextView2:
                InputDialogUtil.showInputDialog(getContext(), "请输入耳号", gongzhuTextView2.getText().toString(), new InputDialogUtil.DialogListener() {
                    @Override
                    public void getText(String s) {
                        if (isHave(s)) {
                            Toast.makeText(getContext(), "已有此公猪", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (clearEditText(gongzhuTextView2, s)) {
                            InfoShowFragment.getPigInfo(null, InfoShowFragment.Clear);
                            return;
                        }
                        InfoShowFragment.getPigInfo(new InfoShowFragment.getInfoSuccessListener() {
                            @Override
                            public void success(String s, String id) {
                                gongzhuTextView2.setText(s);
                                gongzhuTextView2.setTag(id);
                            }
                        }, InfoShowFragment.GongZhuType, s);
                    }
                });
                break;
            case R.id.gongzhuTextView3:
                InputDialogUtil.showInputDialog(getContext(), "请输入耳号", gongzhuTextView3.getText().toString(), new InputDialogUtil.DialogListener() {
                    @Override
                    public void getText(String s) {
                        if (isHave(s)) {
                            Toast.makeText(getContext(), "已有此公猪", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (clearEditText(gongzhuTextView3, s)) {
                            InfoShowFragment.getPigInfo(null, InfoShowFragment.Clear);
                            return;
                        }
                        InfoShowFragment.getPigInfo(new InfoShowFragment.getInfoSuccessListener() {
                            @Override
                            public void success(String s, String id) {
                                gongzhuTextView3.setText(s);
                                gongzhuTextView3.setTag(id);
                            }
                        }, InfoShowFragment.GongZhuType, s);
                    }
                });
                break;
            case R.id.muzhuInput:
                InputDialogUtil.showInputDialog(getContext(), "请输入耳号", muzhuTextView.getText().toString(), new InputDialogUtil.DialogListener() {
                    @Override
                    public void getText(String s) {
                        InfoShowFragment.getPigInfo(new InfoShowFragment.getInfoSuccessListener() {
                            @Override
                            public void success(String s, String id) {
                                muzhuTextView.setText(s);
                                muzhuTextView.setTag(id);
                            }
                        }, InfoShowFragment.MuZhuType, s, SPUtils.getInstance().getString(BaseApplication.FLINEID));
                    }
                });
                break;
            case R.id.gongzhuTextView1Input:
                new ScanDialog.Builder(getContext())
                        .setQrCodeReadListener(new QRCodeReaderView.OnQRCodeReadListener() {
                            @Override
                            public void onQRCodeRead(String text, PointF[] points) {
                                if (isHave(text)) {
                                    Toast.makeText(getContext(), "已有此公猪", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
                                InfoShowFragment.getPigInfo(new InfoShowFragment.getInfoSuccessListener() {
                                    @Override
                                    public void success(String s, String id) {
                                        gongzhuTextView1.setText(s);
                                        gongzhuTextView1.setTag(id);
                                    }
                                }, InfoShowFragment.GongZhuType, text);
                            }
                        }).create().show();
                break;
            case R.id.gongzhuTextView2Input:
                new ScanDialog.Builder(getContext())
                        .setQrCodeReadListener(new QRCodeReaderView.OnQRCodeReadListener() {
                            @Override
                            public void onQRCodeRead(String text, PointF[] points) {
                                if (isHave(text)) {
                                    Toast.makeText(getContext(), "已有此公猪", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
                                InfoShowFragment.getPigInfo(new InfoShowFragment.getInfoSuccessListener() {
                                    @Override
                                    public void success(String s, String id) {
                                        gongzhuTextView2.setText(s);
                                        gongzhuTextView2.setTag(id);
                                    }
                                }, InfoShowFragment.GongZhuType, text);
                            }
                        }).create().show();
                break;
            case R.id.gongzhuTextView3Input:
                new ScanDialog.Builder(getContext())
                        .setQrCodeReadListener(new QRCodeReaderView.OnQRCodeReadListener() {
                            @Override
                            public void onQRCodeRead(String text, PointF[] points) {
                                if (isHave(text)) {
                                    Toast.makeText(getContext(), "已有此公猪", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
                                InfoShowFragment.getPigInfo(new InfoShowFragment.getInfoSuccessListener() {
                                    @Override
                                    public void success(String s, String id) {
                                        gongzhuTextView3.setText(s);
                                        gongzhuTextView3.setTag(id);
                                    }
                                }, InfoShowFragment.GongZhuType, text);
                            }
                        }).create().show();
                break;
            case R.id.peiZhongType:
                new AlertDialog.Builder(getContext())
                        .setTitle("请选择配种类型")
                        .setItems(peizhongTypes, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                peiZhongType.setText(peizhongTypes[i]);
                                peiZhongType.setTag((1 + i) + "");
                            }
                        }).show();

                break;

            case R.id.pzy:

                Intent intent = new Intent();
                intent.setClass(getContext(),ChosePersonActivity.class);
                startActivityForResult(intent,1001);
            break;

            case R.id.subbtn:
                if (muzhuTextView.getTag() == null || peiZhongType.getTag() == null) {
                    Toast.makeText(getContext(), "母猪耳号/类型不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                 if (gongzhuTextView1.getTag() == null && gongzhuTextView2.getTag() == null && gongzhuTextView3.getTag() == null) {
                    Toast.makeText(getContext(), "公猪最少得录入一头", Toast.LENGTH_SHORT).show();
                     return;
                }
                if(pzy.getTag()==null||pzy.getText().toString().equals("")){
                    Toast.makeText(getContext(), "请选择配种员", Toast.LENGTH_SHORT).show();
                    return;
                }

                    String gongzhu1Str = "";
                    String gongzhu1erhao = "";
                    if (gongzhuTextView1.getTag() != null) {
                        gongzhu1Str = gongzhuTextView1.getTag().toString();
                        gongzhu1erhao = gongzhuTextView1.getText().toString();
                    }
                    String gongzhu2Str = "";
                    String gongzhu2erhao = "";
                    if (gongzhuTextView2.getTag() != null) {
                        gongzhu2Str = gongzhuTextView2.getTag().toString();
                        gongzhu2erhao = gongzhuTextView2.getText().toString();
                    }
                    String gongzhu3Str = "";
                    String gongzhu3erhao = "";
                    if (gongzhuTextView3.getTag() != null) {
                        gongzhu3Str = gongzhuTextView3.getTag().toString();
                        gongzhu3erhao = gongzhuTextView3.getText().toString();
                    }
                    if(!addBreedingRecordPresenter.subBreedingRecordItemInfo(
                            muzhuTextView.getTag().toString(),
                            gongzhu1Str,
                            gongzhu2Str,
                            gongzhu3Str,
                            peiZhongType.getTag().toString(),
                            muzhuTextView.getText().toString(),
                            gongzhu1erhao,
                            gongzhu2erhao,
                            gongzhu3erhao,
                            peiZhongType.getText().toString(),
                            pzy.getText().toString(),
                            pzy.getTag().toString()
                    )) {
                        muzhuTextView.setText("请点击扫描录入");
                        muzhuTextView.setTag(null);
                    }
                break;
        }
    }

    //判断是否已有此耳号
    public boolean isHave(String erhao) {
        if (gongzhuTextView1.getText().toString().equals(erhao))
            return true;
        if (gongzhuTextView2.getText().toString().equals(erhao))
            return true;
        if (gongzhuTextView3.getText().toString().equals(erhao))
            return true;
        return false;
    }

    public boolean clearEditText(TextView textView, String s) {
        if (s.equals("")) {
            textView.setText("请点击录入");
            textView.setTag(null);
            return true;
        }
        return false;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1001&&resultCode==100){
            String pid = data.getStringExtra("pid");
            String pname = data.getStringExtra("pname");
            pzy.setText(pname);
            pzy.setTag(pid);
        }
    }
}
